package com.rmfyzxfw.caseprocess.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rmfyzxfw.caseprocess.boot.result.page.Pagination;
import com.rmfyzxfw.caseprocess.model.cmd.DataFieldCmd;
import com.rmfyzxfw.caseprocess.model.converter.DataFieldConverter;
import com.rmfyzxfw.caseprocess.model.param.DataFieldListParam;
import com.rmfyzxfw.caseprocess.model.po.DataField;
import com.rmfyzxfw.caseprocess.model.vo.DataFieldVO;
import com.rmfyzxfw.caseprocess.service.DataFieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@RestController
@RequestMapping("api/v3/dataField")
@RequiredArgsConstructor
@Api(tags = "数据项字段")
public class DataIFieldController {
    private final DataFieldService dataFieldService;
    private final DataFieldConverter dataFieldConverter;

    @PostMapping("add")
    @ApiOperation(value = "数据项添加字段")
    public void addDataItemField(@RequestBody @Valid DataFieldCmd dataFieldCmd) {
        dataFieldService.addDataItemField(dataFieldCmd);
    }

    @PostMapping("remove/{dataFieldId}")
    @ApiOperation(value = "数据项删除字段")
    public void removeDataItemField(@PathVariable String dataFieldId) {
        dataFieldService.remove(Wrappers.<DataField>lambdaQuery().eq(DataField::getDataFieldId, dataFieldId).last("limit 1"));
    }

    @GetMapping("list")
    @ApiOperation(value = "获取数据项字段")
    public Pagination<DataFieldVO> list(@Valid DataFieldListParam param) {
        Page<DataField> page = new Page<>(param.getPageNum(), param.getPageSize());
        Page<DataField> dataFieldPage = dataFieldService.page(page, Wrappers.<DataField>lambdaQuery()
                .eq(DataField::getDataItemId, param.getDataItemId())
                .like(StringUtils.isNotBlank(param.getFieldNameCn()), DataField::getFieldNameCn, param.getFieldNameCn())
                .orderByDesc(DataField::getId));

        return Pagination.build(dataFieldPage, dataFieldConverter::dos2vos);
    }
}
